/**
 * Created by ㅋㅋElsa on 2017/9/24.
 */

layui.define(['form', 'layer','jquery'], function(exports){
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer;

    $.ajax({
        type: "GET",
        url: contextPath + "/personal/info",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $('#name').attr("value",data.name);
            $('#sex').attr("value",data.sex);
            $('#state').attr("value",data.state);
            $('#phone').attr("value",data.username);
            $('#userid').attr("value",data.userid);
            $('#rgsttime').attr("value",data.rgsttime);
            if(data.role=='验船师'){
                $('#field').show();
                $('#be').attr("value",data.ct+" "+data.dq+" "+data.lj);
            }
            if(data.role=='船东'){
                $('#co').show();
                $('#coname').attr("value",data.coname);
            }
            if(data.role=='船厂'){
                $('#co').show();
                $('#shipyard').show();
                $('#coname').attr("value",data.coname);
                $('#fax').attr("value",data.fax);
                $('#zipcode').attr("value",data.zipcode);
                $('#crecode').attr("value",data.crecode);
                $('#ccadr').attr("value",data.ccadr);
                $('#writename').attr("value",data.writename);
            }
        },
        error: function (e) {

        }
    });

    form.on('submit(casegly)', function(data) {
        $.ajax({
            type: "GET",
            url: contextPath + "/personal/info",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                console.log(data);
                if(data.state == "正常"){
                    if(data.role=='验船师'&&(data.work == -1||data.work == 0)){
                        $('#beInfo').attr('class','fa fa-unlock');
                        $('#be').attr("disabled",false);
                        $('#be').attr("class","layui-input");
                        $('#bek').hide();
                        $('#bes1').show();
                        $('#bes2').show();
                        $('#bes3').show();
                        if(data.ct){
                            $('#ctx').attr("selected",true);
                        }
                        if(data.dq){
                            $('#dqx').attr("selected",true);
                        }
                        if(data.lj){
                            $('#ljx').attr("selected",true);
                        }
                    }
                    if(data.role=='船东'){
                        $('#addressInfo').attr('class','fa fa-unlock');
                        $('#coname').attr("disabled",false);
                        $('#coname').attr("class","layui-input");
                    }
                    if(data.role=='船厂'){
                        $('#addressInfo').attr('class','fa fa-unlock');
                        $('#faxInfo').attr('class','fa fa-unlock');
                        $('#zipcodeInfo').attr('class','fa fa-unlock');
                        $('#crecodeInfo').attr('class','fa fa-unlock');
                        $('#ccadrInfo').attr('class','fa fa-unlock');
                        $('#writenameInfo').attr('class','fa fa-unlock');
                        $('#coname').attr("disabled",false);
                        $('#coname').attr("class","layui-input");
                        $('#fax').attr("disabled",false);
                        $('#fax').attr("class","layui-input");
                        $('#zipcode').attr("disabled",false);
                        $('#zipcode').attr("class","layui-input");
                        $('#crecode').attr("disabled",false);
                        $('#crecode').attr("class","layui-input");
                        $('#ccadr').attr("disabled",false);
                        $('#ccadr').attr("class","layui-input");
                        $('#writename').attr("disabled",false);
                        $('#writename').attr("class","layui-input");
                    }

                    $('#nameInfo').attr('class','fa fa-unlock');
                    $('#name').attr("disabled",false);
                    $('#name').attr("class","layui-input");
                    $('#sexInfo').attr('class','fa fa-unlock');
                    $('#sex').attr("disabled",false);
                    $('#sex').attr("class","layui-input");
                    $('#usernameInfo').attr('class','fa fa-unlock');
                    $('#phone').attr("disabled",false);
                    $('#phone').attr("class","layui-input");

                    $('#changee').hide();
                    $('#changeex').show();

                    $('#sexk').hide();
                    $('#sexs').show();

                    if(data.sex == "男"){
                        $("#se option[value='男']").attr("selected", true);
                    } else if(data.sex == "女"){
                        $("#se option[value='女']").attr("selected", true);
                    }

                }else {
                }

                form.render();
            },
            error: function (e) {

            }
        });

        return false;
    });

    form.on('submit(caseglyx)', function(data) {
        var updateuser = new Object();
        $.ajax({
            type: "GET",
            url: contextPath + "/personal/role",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if(data.role=="验船师"){
                    if($("select[name='quiz1'] option:selected").val() == "船体"){
                        updateuser.ct = "船体";
                    } else {
                        updateuser.ct = "";
                    }
                    if($("select[name='quiz2'] option:selected").val() == "电气"){
                        updateuser.dq = "电气";
                    } else {
                        updateuser.dq = "";
                    }
                    if($("select[name='quiz3'] option:selected").val() == "轮机"){
                        updateuser.lj = "轮机";
                    } else {
                        updateuser.lj = "";
                    }
                }
                if(data.role=="船厂"){
                    updateuser.coname = $('#coname').val();
                    updateuser.fax = $('#fax').val();
                    updateuser.zipcode = $('#zipcode').val();
                    updateuser.crecode = $('#crecode').val();
                    updateuser.ccadr = $('#ccadr').val();
                    updateuser.writename = $('#writename').val();
                }
                if(data.role=="船东"){
                    if($('#coname').val()){
                        updateuser.coname = $('#coname').val();
                    } else {
                        updateuser.coname = "";
                    }
                }

                updateuser.name = $('#name').val();
                updateuser.sex = $("select[name='sex'] option:selected").val();
                updateuser.username = $('#phone').val();
                console.log(JSON.stringify(updateuser));

                $.ajax({
                    type:"PUT",
                    url:contextPath+"/personal/update",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(updateuser),
                    dataType:"json",
                    success:function (data) {
                        console.log(data.message);
                        if(data.message == "用户修改成功!"){
                            layer.closeAll();
                            layer.msg('用户修改成功!<br>页面将在1秒后刷新...');
                            setTimeout(function(){window.location.reload();},1000);
                        } else {
                            layer.msg(data.message, {
                                time:false,
                                btn:"确定",
                                btnAlign: 'c'
                            });
                        }
                    },
                    error:function (e) {
                    }
                });
                
            },
            error: function (e) {

            }
        });
        return false;
    });

    exports('PersonalInfo', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});